package org.abratuhi.kvngroupware.chat.mptua;

import java.util.List;
import java.util.Properties;

import org.abratuhi.kvngroupware.chat.gui.GuiChatroom;
import org.abratuhi.kvngroupware.chat.manager.ChatroomManager;
import org.abratuhi.kvngroupware.chat.model.Chatroom;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.network.AbstractResponseMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

public class ChatroomResponseMessageProceeder extends AbstractResponseMessageProceeder{
	LoginManager loginmanager;
	ChatroomManager chatroommanager;
	WidgetManager widgetmanager;
	
	public ChatroomResponseMessageProceeder(MessageSender sender, Properties locale, LoginManager loginmanager, ChatroomManager chatroommanager, WidgetManager widgetmanager){
		super(sender, locale);
		this.loginmanager = loginmanager;
		this.chatroommanager = chatroommanager;
		this.widgetmanager = widgetmanager;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element response = ((Element) message.getRoot().getChildren("response").get(0));
		String type = response.getAttributeValue("type");
		String action = response.getAttributeValue("action");
		String chatroomId = response.getAttributeValue("chatroomId");
		String chatroomTheme = response.getAttributeValue("chatroomTheme");
		String userId = response.getAttributeValue("userId");
		String status = response.getAttributeValue("status");
		String text = response.getText();

		if(type.equals("chatroom")){
			if(status.equals("ok")){
				if(action.equals("create")){
					Chatroom chatroom = new Chatroom(chatroomId, chatroomTheme);
					chatroommanager.put(chatroomId, chatroom);
				}
				else if(action.equals("get")){
					List<Element> chatrooms = response.getChildren("chatroom");
					chatroommanager.clear();
					for(int i=0; i<chatrooms.size();i++){
						Chatroom chatroom = Chatroom.fromElement(chatrooms.get(i));
						chatroommanager.put(chatroomId, chatroom);
					}
				}
				else if(action.equals("delete")){
					chatroommanager.remove(chatroomId);
				}
				else if(action.equals("join")){
					Chatroom chatroom = chatroommanager.get(chatroomId);
					chatroommanager.get(chatroomId).addMember(userId);
					if(userId.equals(loginmanager.getUser().getId())){
						GuiChatroom guichatroom = new GuiChatroom(chatroomId, "Chatroom: "+chatroom.getTheme(), sender, locale, loginmanager, chatroommanager);
						guichatroom.createWindow();
						widgetmanager.put(chatroomId, guichatroom);
					}
				}
				else if(action.equals("leave")){
					if(chatroommanager.get(chatroomId).containsMember(userId))
						chatroommanager.get(chatroomId).removeMember(userId);
					if(loginmanager.getUser().getId().equals(userId)){
						GuiChatroom guichatroom = (GuiChatroom) widgetmanager.get(chatroomId);
						guichatroom.deleteWindow();
						widgetmanager.remove(chatroomId);
					}
				}
				else if(action.equals("send")){
					chatroommanager.get(chatroomId).appendMessage("["+userId+"]: "+text);
				}
			}
		}
	}

}
